package web.base.user.convertor;

import web.base.user.message.UserRequest;
import web.base.user.message.UserResponse;
import web.base.user.message.UserSearchRequest;
import web.base.user.message.UserSearchResponse;
import web.base.user.model.UserEntity;
import web.common.core.collection.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jin Zheng
 * @since 2022-01-03
 */
public class UserConvertor {
	private UserConvertor() {
	}

	public static UserEntity toEntity(UserRequest request) {
		if (request == null) {
			return null;
		}
		UserEntity entity = new UserEntity();
		entity.setId(request.getId());
		entity.setMobile(request.getMobile());
		entity.setName(request.getName());
		entity.setPassword(request.getPassword());
		entity.setState(request.getState());
		return entity;
	}

	public static UserEntity toEntity(UserSearchRequest request) {
		if (request == null || request.getRequest() == null) {
			return new UserEntity();
		}
		return toEntity(request.getRequest());
	}

	public static Page toPage(UserSearchRequest request) {
		if (request == null) {
			return null;
		}
		return new Page(request.getPageSize(), request.getPageIndex());
	}

	public static UserResponse toResponse(UserEntity entity) {
		if (entity == null) {
			return null;
		}
		UserResponse response = new UserResponse();
		response.setId(entity.getId());
		response.setMobile(entity.getMobile());
		response.setName(entity.getName());
		response.setRegisterTime(entity.getRegisterTime());
		response.setLoginTime(entity.getLoginTime());
		response.setState(entity.getState());
		return response;
	}

	public static List<UserResponse> toResponse(List<UserEntity> list) {
		List<UserResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty()) {
			return responseList;
		}
		for (var entity : list) {
			responseList.add(toResponse(entity));
		}
		return responseList;
	}

	public static UserSearchResponse toPage(Page page) {
		UserSearchResponse response = new UserSearchResponse();
		response.setPage(page, toResponse(page.getResults()));
		return response;
	}

}
